# -*- codeing = utf-8 -*-
from selenium import webdriver
import time
import csv
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#创建一个options对象
#创建一个csv文件
f = open('./京东.csv','w',encoding='utf-8',newline='')
mywrite = csv.writer(f)
mywrite.writerow(['价格','标签','链接'])
options = webdriver.ChromeOptions()
# 避免终端下执行代码报错
options.add_experimental_option("excludeSwitches", ['enable-automation',
'enable-logging'])
#京东爬取
URL = "https://www.jd.com/"
#创建一个对象
browser = webdriver.Chrome('./chromedriver.exe',options=options)
#最大
browser.maximize_window()
#请求网页
browser.get(url=URL)
#s搜索商品名
#写死商品，提高速度
goods = '口红'#input('请输入商品：\n')
#定位输入框
browser.find_element(By.CSS_SELECTOR,'#key').send_keys(goods)
#点击搜索
browser.find_element(By.CSS_SELECTOR,'#search > div > div.form > button').click()
#调用js方法，获取整个页面
y = 0
y_max = 7000
while y < y_max:
    y += 500
    #调用js实现页面滚动刷新
    browser.execute_script(f'window.scrollTo(0,{y})')
    WebDriverWait(browser, 5).until(
        EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#search-2014 > div > button'), '搜索')
    )
#获取整个网页的记录
list_phone = browser.find_elements(By.CSS_SELECTOR,'#J_goodsList > ul > li')
for i in list_phone:
    price = i.find_element(By.CSS_SELECTOR,'div > div.p-price > strong').text

    title = i.find_element(By.CSS_SELECTOR,'div > div.p-name.p-name-type-2 > a').text
    href = i.find_element(By.CSS_SELECTOR,'div > div.p-name.p-name-type-2 > a').get_attribute('href')

    mywrite.writerow([price,title,href])


f.close()
